/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.turn_layout_manager;

import com.cdflynn.ohos.library.turn.TurnListContainer;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.Slider;
import ohos.agp.components.Switch;
import ohos.agp.components.Text;

import java.util.ArrayList;

/**
 * 测试功能界面
 *
 * @author:turnlayout
 * @since 2021-04-19
 */
public class MainAbility extends Ability {
    private static final int COUNT = 50;
    private boolean isShow = true;
    private RadioContainer radioContainer1;
    private RadioContainer radioContainer2;
    private Slider slider1;
    private Slider slider2;
    private Switch btnSwitch;
    private TurnListContainer turnView;
    private Text radius;
    private Text peek;

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initListContainer();
    }

    private void initListContainer() {
        turnView = (TurnListContainer) findComponentById(ResourceTable.Id_trunTableView);
        radius = (Text) findComponentById(ResourceTable.Id_slider_radius);
        peek = (Text) findComponentById(ResourceTable.Id_slider_peek);
        Text controls = (Text) findComponentById(ResourceTable.Id_controls);
        DirectionalLayout menu = (DirectionalLayout) findComponentById(ResourceTable.Id_menu);
        radioContainer1 = (RadioContainer) findComponentById(ResourceTable.Id_radioContainer1);
        radioContainer2 = (RadioContainer) findComponentById(ResourceTable.Id_radioContainer2);
        slider1 = (Slider) findComponentById(ResourceTable.Id_slider1);
        slider2 = (Slider) findComponentById(ResourceTable.Id_slider2);
        btnSwitch = (Switch) findComponentById(ResourceTable.Id_btn_switch);

        ArrayList<SampleItem> items = new ArrayList<>();
        for (int ii = 1; ii < COUNT; ii++) {
            items.add(new SampleItem(+ii + ""));
        }
        SampleItemProvider itemProvider = new SampleItemProvider(this, items);
        turnView.setItemProvider(itemProvider);
        turnView.setGravity(TurnListContainer.Gravity.START);
        controls.setClickedListener(component -> {
            if (isShow) {
                menu.setVisibility(Component.HIDE);
                isShow = false;
            } else {
                menu.setVisibility(Component.VISIBLE);
                isShow = true;
            }
        });
        addListener();
    }

    private void addListener() {
        radioContainer1.setMarkChangedListener((radioContainer, checkId) -> {
            switch (checkId) {
                case 1:
                    turnView.setGravity(TurnListContainer.Gravity.END);
                    break;
                default:
                    turnView.setGravity(TurnListContainer.Gravity.START);
                    break;
            }
        });

        radioContainer2.setMarkChangedListener((radioContainer, checkId) -> {
            switch (checkId) {
                case 1:
                    turnView.setOrientation(TurnListContainer.Orientation.HORIZONTAL);
                    break;
                default:
                    turnView.setOrientation(TurnListContainer.Orientation.VERTICAL);
                    break;
            }
        });

        slider1.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean isB) {
                turnView.setRadius(i);
                radius.setText("Radius:" + i);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
        slider2.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean isB) {
                turnView.setPeekDistance(progress);
                peek.setText("Peek:" + progress);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
        btnSwitch.setCheckedStateChangedListener((absButton, b) -> turnView.setRotate(b));
    }
}
